﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Codemonk.Common.Emit;
using Codemonk.Common.Diagnostic;

namespace Codemonk.CloudBox.UnitTests.Emit
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void EmitTestMethod()
        {
            TypeAccessor accessor = new TypeAccessor(typeof(TestClass));
            TestClass tc = new TestClass(90);
            Console.WriteLine(accessor.ExecuteFunc(tc, "AAA", new object[] { 12, 13 }));
            Console.WriteLine(accessor.ExecuteFunc(tc, "AAC", new object[] { 12, tc }));
            Console.WriteLine(accessor.GetProperty(tc, "BaseValue"));
            accessor.SetProperty(tc, "BaseValue", 32);
            Console.WriteLine(accessor.GetProperty(tc, "BaseValue"));
            var v = accessor.Create(12);
            TypeAccessor<TestClass> atc = new TypeAccessor<TestClass>();
            v = atc.Create(45);

            TypeAccessorFactory af1 = new TypeAccessorFactory();
            TypeAccessorFactory af2 = new TypeAccessorFactory();
            Console.WriteLine(ActionTimer.Time(() => af1.Get("System.Net.IPEndPoint, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")));
            Console.WriteLine(ActionTimer.Time(() => af1.GetByType(typeof(System.Net.IPEndPoint))));
            var ip1 = af1.GetByType(typeof(System.Net.IPEndPoint)).Create(System.Net.IPAddress.Loopback, 90);
            var ip2 = af1.GetByType(typeof(System.Net.IPEndPoint)).Create(145678L, 90);
        }
    }

    public class TestClass
    {
        private int baseValue;

        public int BaseValue
        {
            get { return baseValue; }
            set { baseValue = value; }
        }



        public TestClass(int baseValue)
        {
            this.baseValue = baseValue;
        }
        public int AAA(int a, int b)
        {
            Console.WriteLine("int,int");
            return baseValue + a + b;
        }
        public void AAB(int a, object b)
        {
            Console.WriteLine("int,object");
        }
        public void AAC(int a, TestClass b)
        {
            Console.WriteLine("int,testclass");
        }
    }
}
